package com.jukte.recipe.db;

import java.util.ArrayList;
import java.util.List;

import com.jukte.recipe.models.Category;
import com.jukte.recipe.models.Direction;
import com.jukte.recipe.models.IngGroup;
import com.jukte.recipe.models.Ingredient;
import com.jukte.recipe.models.Note;
import com.jukte.recipe.models.Recipe;
import com.jukte.recipe.models.SubCategory;
import com.jukte.recipe.models.Tips;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class DBAdapter {
	public static final String AUTHORITY = "quinoa";//specific for our app, will be specified in manifest 
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
	private Context mContext;
	
	public DBAdapter(Context ctx) {
		this.mContext = ctx;
	}
	
	public boolean recreateTable(String tableName)
	{
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, tableName);
		int value=mContext.getContentResolver().delete(contentUri, null, null);
		return value>0;
	}	
	
	// CHECK VERSION
	public int checkVersion(){
		int version = 0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "db_versions");
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, null, null, null);
		if(cursor.moveToFirst()){
			version = cursor.getInt(cursor.getColumnIndex("version"));
		}
		cursor.close();
		return version;
	}
	
	// UPDATE VERSION
	public void updateVersion(int ver){
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "db_versions");
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, null, null, null);
		if(cursor.moveToFirst()){
			ContentValues values = new ContentValues();
			values.put("version", ver);
			mContext.getContentResolver().update(contentUri, values, null, null);
		}else{
			ContentValues values = new ContentValues();
			values.put("version", ver);
			mContext.getContentResolver().insert(contentUri, values);
		}
		cursor.close();
	}
		
	// ADD DATA TO DB
	public long addCategories(int id, String name, String img_path){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "categories");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("name", name);
		values.put("img_path", img_path);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addCustomIngredients(int id, String text, int recipe_id){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "custom_ingredients");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("text", text);
		values.put("recipe_id", recipe_id);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addDirections(int id, String text, int recipe_id, int ordering){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "directions");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, "id="+id+" AND text ='"+text+"' AND recipe_id="+recipe_id, null, null);
		if(cursor.moveToFirst()){
			return cursor.getInt(cursor.getColumnIndex("id"));
		}
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("text", text);
		values.put("recipe_id", recipe_id);
		values.put("ordering", ordering);

		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addIngGroup(int id, String name, int recipe_id){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "ing_group");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("name", name);
		values.put("recipe_id", recipe_id);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addIngredients(int id, int is_added_cart, String text, int ing_group_id){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "ingredients");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, "id="+id+" AND text ='"+text+"' AND ing_group_id="+ing_group_id, null, null);
		if(cursor.moveToFirst()){
			return cursor.getInt(cursor.getColumnIndex("id"));
		}
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("is_added_cart", is_added_cart);
		values.put("text", text);
		values.put("ing_group_id", ing_group_id);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}	
		
	public long addNotes(String noteText,int recipeId){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "notes");
		
		ContentValues values = new ContentValues();
		values.put("text", noteText);
		values.put("recipe_id", recipeId);
		values.put("ordering", 0);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addRecipeSubCategoryRelation(int id, int recipe_id, int sub_category_id){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipe_sub_category_relation");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("recipe_id", recipe_id);
		values.put("sub_category_id", sub_category_id);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addRecipe(int has_pdf, int is_added_cart, int id, String name, String description, 
			String serves, String img_path, String thumbnail_img_path, int is_fav){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		
		ContentValues values = new ContentValues();
		values.put("has_pdf", has_pdf);
		values.put("is_added_cart", is_added_cart);
		values.put("id", id);
		values.put("name", name);
		values.put("description", description);
		values.put("serves", serves);
		values.put("img_path", img_path);
		values.put("thumbnail_img_path", img_path);
		values.put("is_fav", is_fav);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public long addSubCategories(int id, int category_id, String name){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "sub_categories");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("category_id", category_id);
		values.put("name", name);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
	
	public long addTips(int id, String text, int recipe_id, int ordering){
		long idAdded=0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "tips");
		
		ContentValues values = new ContentValues();
		values.put("id", id);
		values.put("text", text);
		values.put("recipe_id", recipe_id);
		values.put("ordering", ordering);
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public void updateFav(int favValue,int recipeId){
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		ContentValues values = new ContentValues();
		values.put("is_fav", favValue);
		
		mContext.getContentResolver().update(contentUri, values, "id="+recipeId, null);
	}
		
	public List<Ingredient> getAllIngredients(int recipeId){
		List<Ingredient> items=new ArrayList<Ingredient>();
		List<IngGroup> groups=getAllIngGroupByRecipeId(recipeId);
		for(int i=0;i<groups.size();i++)
			items.addAll(groups.get(i).getIngredients());
		return items;
	}
		
	public List<Ingredient> getIngredientsAddedtoCart(int recipeId){
		List<Ingredient> items=new ArrayList<Ingredient>();
		List<IngGroup> groups=getAllIngGroupByRecipeId(recipeId);
		for(int i=0;i<groups.size();i++)
			for(int j=0;j<groups.get(i).getIngredients().size();j++)
			{
				if(groups.get(i).getIngredients().get(j).getIsAddedToCart()==1)
					items.add(groups.get(i).getIngredients().get(j));
			}
		return items;
	}
		
	public List<Ingredient> getCustomIngredients(int recipeId)
	{
		List<Ingredient> ingredients=new ArrayList<Ingredient>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "custom_ingredients");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, new String[]{"id","text","recipe_id"}, "recipe_id="+recipeId, null, null);
		if(cursor.moveToFirst()){
	        do{
	        	Ingredient ingredient=new Ingredient();
	        	ingredient.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	ingredient.setText(cursor.getString(cursor.getColumnIndex("text")));
	        	ingredient.setRecipe_id(cursor.getInt(cursor.getColumnIndex("recipe_id")));
	        	ingredients.add(ingredient);
	        }while(cursor.moveToNext());
	    }
		cursor.close();
		return ingredients;
	}
		
	public List<Ingredient> getAllIngredients_in_Cart(int recipeId){
		List<Ingredient> items=new ArrayList<Ingredient>();
		items=getIngredientsAddedtoCart(recipeId);
		items.addAll(getCustomIngredients(recipeId));
		return items;
	}
		
	public Recipe getRecipeDetailsByDeep(int recipeId)
	{
		Recipe recipe=new Recipe();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, "id="+recipeId, null, null);
		if(cursor.moveToFirst()){
	        do{
	            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
	        	recipe.setDescription(cursor.getString(cursor.getColumnIndex("description")));
	        	recipe.setServes(cursor.getString(cursor.getColumnIndex("serves")));
	        	recipe.setImgPath(cursor.getString(cursor.getColumnIndex("img_path")));
	        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
	        	recipe.setIsFav(cursor.getInt(cursor.getColumnIndex("is_fav")));
	        	recipe.setHasPdf(cursor.getInt(cursor.getColumnIndex("has_pdf")));
	        	recipe.setIsAddedToCart(cursor.getInt(cursor.getColumnIndex("is_added_cart")));
	        	List<IngGroup> groups=getAllIngGroupByRecipeId(recipeId);
	        	recipe.setIngGroups(groups);
	        	List<Direction> directions=getAllDirectionsByRecipeId(recipeId);
	        	recipe.setDirections(directions);
	        	List<Note> notes=getAllNotesByRecipeId(recipeId);
	        	recipe.setNotes(notes);
	        	List<Tips> tips=getAllTipsByRecipeId(recipeId);
	        	recipe.setTips(tips);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return recipe;
	}
		
		
	private List<Tips> getAllTipsByRecipeId(int recipeId) {
		List<Tips> tips=new ArrayList<Tips>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "tips");
		Cursor tipsCursor = mContext.getContentResolver().query(contentUri, null, "recipe_id="+recipeId, null, null);
		if(tipsCursor.moveToFirst()){
	        do{
	        	Tips tip=new Tips();
	        	int tipId=tipsCursor.getInt(tipsCursor.getColumnIndex("id"));
	        	tip.setId(tipId);
	        	tip.setText(tipsCursor.getString(tipsCursor.getColumnIndex("text")));
	        	tip.setRecipeId(tipsCursor.getInt(tipsCursor.getColumnIndex("recipe_id")));
	        	tip.setOrdering(tipsCursor.getInt(tipsCursor.getColumnIndex("ordering")));
	        	tips.add(tip);
	        }while(tipsCursor.moveToNext());
		}
		tipsCursor.close();
		return tips;
	}

	private List<Note> getAllNotesByRecipeId(int recipeId) {
		List<Note> notes=new ArrayList<Note>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "notes");
		
		Cursor notesCursor = mContext.getContentResolver().query(contentUri, null, "recipe_id="+recipeId, null, null);		
		if(notesCursor.moveToFirst()){
	        do{
	        	Note note=new Note();
	        	int noteId=notesCursor.getInt(notesCursor.getColumnIndex("id"));
	        	note.setId(noteId);
	        	note.setText(notesCursor.getString(notesCursor.getColumnIndex("text")));
	        	note.setRecipeId(notesCursor.getInt(notesCursor.getColumnIndex("recipe_id")));
	        	note.setOrdering(notesCursor.getInt(notesCursor.getColumnIndex("ordering")));
	        	notes.add(note);
	        }while(notesCursor.moveToNext());
	    }
        notesCursor.close();
		return notes;
	}

	private List<Direction> getAllDirectionsByRecipeId(int recipeId) {
		List<Direction> directions=new ArrayList<Direction>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "directions");
		
		Cursor directionCursor = mContext.getContentResolver().query(contentUri, null, "recipe_id="+recipeId, null, null);
		if(directionCursor.moveToFirst()){
	        do{
	        	Direction direction=new Direction();
	        	int ingGroupId=directionCursor.getInt(directionCursor.getColumnIndex("id"));
	        	direction.setId(ingGroupId);
	        	direction.setText(directionCursor.getString(directionCursor.getColumnIndex("text")));
	        	direction.setRecipeId(directionCursor.getInt(directionCursor.getColumnIndex("recipe_id")));
	        	direction.setOrdering(directionCursor.getInt(directionCursor.getColumnIndex("ordering")));
	        	directions.add(direction);
	        }while(directionCursor.moveToNext());
	        directionCursor.close();
	    }
		return directions;
		
	}

	private List<IngGroup> getAllIngGroupByRecipeId(int recipeId) {
		List<IngGroup> temp=new ArrayList<IngGroup>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "ing_group");
		
		Cursor ingGroupCursor = mContext.getContentResolver().query(contentUri, null, "recipe_id="+recipeId, null, null);
		if(ingGroupCursor.moveToFirst()){
	        do{
	        	IngGroup group=new IngGroup();
	        	int ingGroupId=ingGroupCursor.getInt(ingGroupCursor.getColumnIndex("id"));
	        	group.setId(ingGroupId);
	        	group.setName(ingGroupCursor.getString(ingGroupCursor.getColumnIndex("name")));
	        	group.setRecipeId(ingGroupCursor.getInt(ingGroupCursor.getColumnIndex("recipe_id")));
	        	group.setIngredients(getAllIngredientsByGroupId(ingGroupId));
	        	temp.add(group);
	        }while(ingGroupCursor.moveToNext());
	    }
        ingGroupCursor.close();
		return temp;
	}

	private List<Ingredient> getAllIngredientsByGroupId(int ingGroupId) {
		List<Ingredient> ingredients=new ArrayList<Ingredient>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "ingredients");
		
		Cursor ingredientCursor = mContext.getContentResolver().query(contentUri, null, "ing_group_id="+ingGroupId, null, null);
		if(ingredientCursor.moveToFirst()){
	        do{
	        	Ingredient ingredient=new Ingredient();
	        	ingredient.setId(ingredientCursor.getInt(ingredientCursor.getColumnIndex("id")));
	        	ingredient.setText(ingredientCursor.getString(ingredientCursor.getColumnIndex("text")));
	        	ingredient.setIngGroupId(ingredientCursor.getInt(ingredientCursor.getColumnIndex("ing_group_id")));
	        	ingredient.setIsAddedToCart(ingredientCursor.getInt(ingredientCursor.getColumnIndex("is_added_cart")));
	        	ingredients.add(ingredient);
	        }while(ingredientCursor.moveToNext());
	    }
        ingredientCursor.close();
		return ingredients;
	}

	public List<Recipe> getAllCartRecipeList()
	{
		List<Recipe> recipes=new ArrayList<Recipe>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, new String[]{"id", "name", "thumbnail_img_path"}, "is_added_cart="+1, null, null);
		if(cursor.moveToFirst()){
	        do{
	        	Recipe recipe=new Recipe();
	            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
	        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
	        	recipes.add(recipe);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return recipes;
	}
		
		
	public List<Recipe> getSearchResult(String searchKey)
	{
		List<Recipe> recipes=new ArrayList<Recipe>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, new String[]{"id", "name", "thumbnail_img_path"}, "name LIKE '%"+searchKey+"%'", null, null);
		if(cursor.moveToFirst()){
	        do{
	        	Recipe recipe=new Recipe();
	            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
	        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
	        	recipes.add(recipe);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return recipes;
	}
		
		
	public List<Recipe> getAllFav()
	{
		List<Recipe> recipes=new ArrayList<Recipe>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, new String[]{"id", "name", "thumbnail_img_path"}, "is_fav ="+1, null, null);
		if(cursor.moveToFirst()){
	        do{
	        	Recipe recipe=new Recipe();
	            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
	        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
	        	recipes.add(recipe);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return recipes;
	}
		
	public List<Recipe> getAllRecipeCatId(int catId)
	{
		String[] selectionVaribles={String.valueOf(catId)};
		List<Recipe> recipes=new ArrayList<Recipe>();
		/*String sqlString="select RECIPES.id,RECIPES.name,RECIPES.thumbnail_img_path,RECIPE_SUB_CATEGORY_RELATION.sub_category_id" +
				"		from RECIPE_SUB_CATEGORY_RELATION,RECIPES " +
				"		where RECIPE_SUB_CATEGORY_RELATION.recipe_id=RECIPES.id " +
				"		AND	RECIPE_SUB_CATEGORY_RELATION.sub_category_id " +
				"		IN (SELECT id FROM SUB_CATEGORIES WHERE category_id=?)";
		cursor=getHelper().executeRawQuery(sqlString, selectionVaribles);
		if (cursor != null)
		{
			if(cursor.getCount()>0)
				
				if(cursor.moveToFirst()){
			        do{
			        	Recipe recipe=new Recipe();
			            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
			        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
			        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
			        	
			        	int sub_cat_id = cursor.getInt(cursor.getColumnIndex("sub_category_id"));
			        	if(sub_cat_id == 3 || sub_cat_id==5){
			        		recipe.setVegetarian(true);
			        	}else{
			        		recipe.setVegetarian(false);
			        	}
			        	
			        	recipes.add(recipe);
			        }while(cursor.moveToNext());
			        cursor.close();
			    }

			
		
		}*/
		
		return recipes;
	}
	
	public List<Recipe> getAllRecipeBySubCatId(int subId)
	{
		String[] selectionVaribles={String.valueOf(subId)};
		List<Recipe> recipes=new ArrayList<Recipe>();
		/*String sqlString="select RECIPES.id,RECIPES.name,RECIPES.thumbnail_img_path" +
				"		from RECIPE_SUB_CATEGORY_RELATION,RECIPES " +
				"		where RECIPE_SUB_CATEGORY_RELATION.sub_category_id=?" +
				"		AND RECIPE_SUB_CATEGORY_RELATION.recipe_id=RECIPES.id";
		cursor=getHelper().executeRawQuery(sqlString,selectionVaribles);
		if (cursor != null)
		{
			if(cursor.getCount()>0)
				
				if(cursor.moveToFirst()){
			        do{
			        	Recipe recipe=new Recipe();
			            recipe.setId(cursor.getInt(cursor.getColumnIndex("id")));
			        	recipe.setName(cursor.getString(cursor.getColumnIndex("name")));
			        	recipe.setImgPathThumnail(cursor.getString(cursor.getColumnIndex("thumbnail_img_path")));
			        	recipes.add(recipe);
			        }while(cursor.moveToNext());
			        cursor.close();
			    }

			
		
		}*/
		
		return recipes;
	}
		
		
	public List<SubCategory> getAllSubCategoriesByCategoryId(int id)
	{
		List<SubCategory> subCategories=new ArrayList<SubCategory>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "sub_categories");
		
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, "category_id ="+id, null, null);
		if(cursor.moveToFirst()){
	        do{
	        	SubCategory subCat=new SubCategory();
	            
	        	subCat.setId(cursor.getInt(cursor.getColumnIndex("id")));
	        	subCat.setName(cursor.getString(cursor.getColumnIndex("name")));
	        	subCat.setCategoryId(cursor.getInt(cursor.getColumnIndex("category_id")));
	            
	        	subCategories.add(subCat);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return subCategories;
	}
		
	public List<Category> getAllcategories()
	{
		List<Category> categories=new ArrayList<Category>();
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "categories");
		Cursor cursor = mContext.getContentResolver().query(contentUri, null, null, null, null);
		if(cursor.moveToFirst()){
	        do{
	            Category cat=new Category();
	            
	            cat.setId(cursor.getInt(cursor.getColumnIndex("id")));
	            cat.setCategoryName(cursor.getString(cursor.getColumnIndex("name")));
	            cat.setImgPath(cursor.getString(cursor.getColumnIndex("img_path")));
	            
	            categories.add(cat);
	        }while(cursor.moveToNext());
	    }
        cursor.close();
		return categories;
	}

	public void updateCart(int recipeId,int i,List<Ingredient> ingredients)
	{
		updateRecipeAddToCart(recipeId, i);
		updateIngredientsAddToCart(ingredients);
	}
		
	public void updateRecipeAddToCart(int recipeId, int i) {
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "recipes");
		ContentValues values = new ContentValues();
		values.put("is_added_cart", i);
		
		mContext.getContentResolver().update(contentUri, values, "id="+recipeId, null);
	}

	private void updateIngredientsAddToCart(List<Ingredient> ingredients) {
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "ingredients");
		
		for (Ingredient ingredient : ingredients) {
			ContentValues values = new ContentValues();
			values.put("is_added_cart", ingredient.getIsAddedToCart());
			mContext.getContentResolver().update(contentUri, values, "id="+ingredient.getId(), null);
		}
	}
		
		
	public long AddCustomIngredient(Ingredient ingredient)
	{
		long idAdded = 0;
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "custom_ingredients");
		
		ContentValues values = new ContentValues();
		values.put("text", ingredient.getText());
		values.put("recipe_id", ingredient.getRecipe_id());
		
		Uri resultUri=mContext.getContentResolver().insert(contentUri, values);
		idAdded=Long.parseLong(resultUri.getLastPathSegment());
		return idAdded;
	}
		
	public void deleteCustomIngredient(Ingredient ingredient){
		Uri contentUri = Uri.withAppendedPath(CONTENT_URI, "custom_ingredients");
		mContext.getContentResolver().delete(contentUri, "id="+ingredient.getId(), null);
	}
}